package com.techsoul.module.pms.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.util.lang.DateUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialDeliveryLineSaveParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialDeliverySaveParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialDeliverySearchParam;
import com.techsoul.module.pms.service.ITbWmsMaterialDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 出库单 Controller
 *
 * @author liuen
 * @date 2022-01-25
 */
@Api(tags = "出库单")
@RestController
@RequestMapping("/web/tbWmsMaterialDelivery")
public class TbWmsMaterialDeliveryController {

    @Autowired
    private Result result;
    
    @Autowired
    private ITbWmsMaterialDeliveryService service;

  //  @Autowired
 //   private ITFoFreightOrderService iTFoFreightOrderService;
    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody TbWmsMaterialDeliverySaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:update')")
    @PutMapping
    public Result update(@Valid @RequestBody TbWmsMaterialDeliverySaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }



    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:search')")
    @GetMapping("/getOrderDetail/{id}")
    public Result getOrderDetail(@PathVariable("id") String id) {

        return result.success(this.service.getOrderDetail(id));
    }



    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody TbWmsMaterialDeliverySearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody TbWmsMaterialDeliverySearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        if(!StringUtils.isNull(params.getDateRange())){
            Date daterange = DateUtils.parseDate(params.getDateRange());
            Date beginDay = DateUtils.getDateBefore(daterange,1);
            String bgdate = DateUtils.formatDate(beginDay);
            params.setCreateTimeBegin(bgdate);
            Date endday = DateUtils.getDateAfter(daterange,1);
            String enddate = DateUtils.formatDate(endday);
            params.setCreateTimeEnd(enddate);
        }

        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("导入模板")
    @PostMapping("/importTemplate")
    public Result importTemplate() {
        return result.success(this.service.importTemplate());
    }

    @ApiOperation("导入")
    @PostMapping("/importData")
    public Result importData(MultipartFile file, boolean isUpdateSupport) {
        return result.success(this.service.importData(file, isUpdateSupport));
    }

   /* @ApiOperation("收货单接受")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:insert')")
    @PostMapping("/insertToolsAll")
    public Result insertToolsAll(@RequestBody TbWmsMaterialDeliverySaveParam params){
        return result.success(service.insertALLToolsItem(params));
    }
    */
    @ApiOperation("收货单接受")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:insert')")
    @PostMapping("/insertAll")
    public Result insertAll(@RequestBody TbWmsMaterialDeliverySaveParam params){

        return result.success(service.insertALLItem(params));
    }

    @ApiOperation("收货单接受")
    @PreAuthorize("hasAnyAuthority('web:tbWmsMaterialDelivery:insert')")
    @PostMapping("/insertAllList")
    public Result insertAllList(@RequestBody JSONObject jsonObject){
        String str = "";
        List<TbWmsMaterialDeliverySaveParam> paramsList = new ArrayList<>();
        jsonObject.forEach((key, value) -> {
            List<TbWmsMaterialDeliverySaveParam> params = JSONArray.parseArray("["+value.toString()+"]",TbWmsMaterialDeliverySaveParam.class);
            paramsList.add(params.get(0));
        });

        for(TbWmsMaterialDeliverySaveParam params : paramsList){
            str = service.insertALLItem(params);
        }

        return result.success(str);
    }
}
